<cfcomponent displayname="ticketController" extends="ModelGlue.unity.controller.Controller" output="false">

	<cffunction name="setTicketService" access="public" returntype="void" output="false">	
		<cfargument name="ticketService" type= "any" required="true">	
		<cfset variables.ticketService = arguments.ticketService />
	</cffunction>

	<cffunction name="getTicketService" access= "public" returntype="any" output="false">	
		<cfreturn variables.ticketService />
	</cffunction>

	<cffunction name="setProjectService" access="public" returntype="void" output="false">	
		<cfargument name="projectService" type= "any" required="true">	
		<cfset variables.projectService = arguments.projectService />
	</cffunction>

	<cffunction name="getProjectService" access= "public" returntype="any" output="false">	
		<cfreturn variables.projectService />
	</cffunction>

	<cffunction name="seticsService" access="public" returntype="void" output="false">	
		<cfargument name="icsService" type= "any" required="true">	
		<cfset variables.icsService = arguments.icsService />
	</cffunction>

	<cffunction name="geticsService" access= "public" returntype="any" output="false">	
		<cfreturn variables.icsService />
	</cffunction>

	<cffunction name="dumpvar">
		<cfargument name="daVar"><cfargument name="abort" default="true">
		<cfdump var="#daVar#"><cfif arguments.abort><cfabort /></cfif>
	</cffunction>

	<cffunction name="getProjectParams" access="public" returntype="any" hint="gets ticket">
	  <cfargument name="event" type="any">
		<cfset var params = structNew() />
		<cfset var projectId = val(arguments.event.getValue("projectId",0)) />
		<cfif projectId gt 0>
			<cfset params["projectId"] = projectId />
		</cfif>
		<cfreturn params />
	</cffunction>

	<cffunction name="getTicketParams" access="public" returntype="any" hint="gets ticket">
	  <cfargument name="event" type="any">
		<cfset var params = structNew() />
		<cfset var ticketId = val(arguments.event.getValue("ticketId",0)) />
		<cfif ticketId gt 0>
			<cfset params["ticketId"] = ticketId />
		</cfif>
		<cfreturn params />
	</cffunction>

	<cffunction name="getTicketById" access="public" returntype="any" hint="gets ticket">
	  <cfargument name="event" type="any">
	  <cfscript>
			var projectService = getProjectService();
			var ticketService = getTicketService();
			var pparams = getProjectParams(arguments.event);
			var tparams = getTicketParams(arguments.event);
			var ticket = ticketService.getTicketById(arguments.event.getValue("ticketId"));
			var priorityQuery = projectService.getProjectPrioritiesQuery(pparams);
			var componentQuery = projectService.getProjectComponentsQuery(pparams);
			var versionQuery = projectService.getProjectVersionsQuery(pparams);
			var milestoneQuery = projectService.getProjectMilestonesQuery(pparams);
			var resolutionQuery = ticketService.getTicketResolutionsQuery(pparams);
			var statusQuery = ticketService.getTicketStatusQuery(pparams);
			var typeQuery = ticketService.getTicketTypeQuery(pparams);
			var severityQuery = ticketService.getTicketSeverityQuery(pparams);
			var commentsQuery = ticketService.getTicketCommentsQuery(tparams);
			var attachmentsQuery = ticketService.getTicketAttachmentsQuery(tparams);
			arguments.event.setValue("ticket",ticket);
			arguments.event.setValue("priorityQuery",priorityQuery);
			arguments.event.setValue("componentQuery",componentQuery);
			arguments.event.setValue("versionQuery",versionQuery);
			arguments.event.setValue("milestoneQuery",milestoneQuery);
			arguments.event.setValue("resolutionQuery",resolutionQuery);
			arguments.event.setValue("statusQuery",statusQuery);
			arguments.event.setValue("severityQuery",severityQuery);
			arguments.event.setValue("typeQuery",typeQuery);
			arguments.event.setValue("commentsQuery",commentsQuery);
			arguments.event.setValue("attachmentsQuery",attachmentsQuery);
		</cfscript>
	</cffunction>

	<cffunction name="getTickets" access="public" returntype="any" hint="gets ticket">
	  <cfargument name="event" type="any">
	  <cfscript>
			arguments.event.setValue("tickets",getTicketService().getTicketsQuery());
		</cfscript>
	</cffunction>

	<cffunction name="getCalendarIcs" access="public" returntype="any">
	  <cfset var calendar = geticsService().createCalendar() />
	  <cfset var eventparams = structNew() />
	  <cfset var event = "" />
	  <cfset var tickets = structNew() />
	  <cfset var ticketsArray = "" />
	  <cfset ticketsArray = getTicketService().getEventsArray(tickets) />
	  <cfset calendar.addCalendarEventsArray(ticketsArray)>
	  <cfoutput>#calendar.print()#</cfoutput><cfabort>
	</cffunction>

</cfcomponent>